<?php
/**
 * Created by 上海互教教育科技有限公司.
 * User: 刘红伟
 * QQ  : 454303753
 * Date: 2017/5/24 0024
 * Time: 上午 10:11
 */

namespace App\Http\Controllers\v1;
use App\Hairdresser;
use App\HairdresserItem;
use App\Levels;
use Illuminate\Support\Facades\DB;
use Laravel\Lumen\Routing\Controller as BaseController;
use Mockery\Exception;
use Illuminate\Http\Request;

class HairdresserController extends BaseController
{
    public function add(Request $request)
    {

        if($request)
        {

            DB::beginTransaction();
            try {
                $time   =   time();
                $hairdresserOBJ             =   new Hairdresser();
                $hairdresserOBJ->name       = $request->name;
                $hairdresserOBJ->level_id   = $request->level_id;
                $hairdresserOBJ->picture    = $request->picture;
                $hairdresserOBJ->create_at  = $time;
                if($hairdresserOBJ->save())
                {

                    $hairdresserItemOBJ     =   new HairdresserItem();
                    $hairdresserItemOBJ->hairdresser_id =   $hairdresserOBJ->hairdresser_id;
                    $hairdresserItemOBJ->item_id        =   $request->item_id;
                    $hairdresserItemOBJ->price          =   $request->price;
                    $hairdresserItemOBJ->subscribe      =   $request->subscribe;
                    $hairdresserItemOBJ->create_at      =   $time;
                    $hairdresserItemOBJ->update_at      =   $time;
                    $hairdresserItemOBJ->save();

                }else
                {
                    throw new Exception('写入失败');

                }
                DB::commit();
            } catch (Exception $e){
                DB::rollback();
                return response()->json(array('status'=>0),200);
            }
            return response()->json(array('status'=>1),200);

        }


    }

    /**
     * 获取单个美发师记录
     * @param $hairdresser_id
     */
    public function get_data($hairdresser_id)
    {
        $data   =   [];
        if($hairdresser_id>0)
        {
            $hairdresser_one           =   Hairdresser::find($hairdresser_id);
            if($hairdresser_one)
            {
                $hairdresserItem_one    =   HairdresserItem::where('hairdresser_id',$hairdresser_id)->first();
                if($hairdresserItem_one)
                {
                    $data['hairdresser']    =   $hairdresser_one;
                    $data['hairdresser_item']    =   $hairdresserItem_one;
                    return response()->json($data,200);
                }

            }
        }
        return response()->json((object)array(),200);
    }

    /**
     * 获取所有
     */
    public function get_all()
    {
        $data               =   array();
        $hairdresser_ids    =   array();
        $level_ids          =   array();

        $hairdressers       =   Hairdresser::all();
        if($hairdressers)
        {
            foreach($hairdressers as $v)
            {
                $hairdresser_ids[$v->hairdresser_id]    =   $v->hairdresser_id;
                $level_ids[$v->hairdresser_id]          =   $v->level_id;
            }
            $hairdresserItems   =   HairdresserItem::all()->whereIn('hairdresser_id',$hairdresser_ids);
            $levels             =   Levels::all()->whereIn('level_id',$level_ids);
            if($hairdresserItems)
            {
                foreach($hairdresserItems as $v)
                {
                    $data['hairdresserItems'][$v->hairdresser_id]   =   $v;
                }
            }

            if($levels)
            {
                foreach($levels as $v)
                {
                    $data['levels'][$v->level_id]   =   $v;
                }
            }
            $data['hairdressers']   =   $hairdressers;
            return response()->json($data,200);

        }
        return response()->json($data,200);

    }

    /**
     * @param $item_id
     */
    public function get_item_all(Request $request)
    {
        $item_id           =    $request->item_id;
        $hairdresser_ids   =    [];
        $data              =    [];
        $levels             =   Levels::all();
        $level_hairdresser  =   [];
        $hairdresser_item   =   [];
        $level_data         =   [];
        if($levels)
        {
            $data['levels']   =  $levels;
            if($item_id>0)
            {
                $hairdresserItems   =   HairdresserItem::all()->where('item_id',$item_id);
                if($hairdresserItems)
                {
                    foreach($hairdresserItems as $v)
                    {
                        $hairdresser_ids[$v->hairdresser_id]    =   $v->hairdresser_id;
                        $hairdresser_item[$v->hairdresser_id]    =   $v;
                    }


                    if($hairdresser_ids)
                    {
                        $hairdressers   =   Hairdresser::all()->whereIn('hairdresser_id',$hairdresser_ids);
                        if($hairdressers)
                        {
                            foreach($hairdressers as $k=>$v)
                            {

                                if(isset($hairdresser_item[$v->hairdresser_id]) && !empty($hairdresser_item[$v->hairdresser_id]))
                                {
                                    $level_hairdresser[$v->level_id][$k]    = $v;
                                    $level_hairdresser[$v->level_id][$k]['item_id']    = $hairdresser_item[$v->hairdresser_id]['item_id'];
                                    $level_hairdresser[$v->level_id][$k]['price']    = $hairdresser_item[$v->hairdresser_id]['price'];
                                    $level_hairdresser[$v->level_id][$k]['subscribe']    = $hairdresser_item[$v->hairdresser_id]['subscribe'];
                                    $level_hairdresser[$v->level_id][$k]['subscribe']    = $hairdresser_item[$v->hairdresser_id]['subscribe'];
                                }

                            }

                        }
                    }
                    foreach($levels as $k=>$v)
                    {
                        $level_data[$k]['level_id']     =   $v->level_id;
                        $level_data[$k]['level_name']   =   $v->name;
                        if(isset($level_hairdresser[$v->level_id]) && !empty($level_hairdresser[$v->level_id]))
                        {
                            $level_data[$k]['hairdresser']   =   $level_hairdresser[$v->level_id];
                            $level_data[$k]['is_hairdresser']  =   true;
                        }else
                        {
                            $level_data[$k]['is_hairdresser']  =   false;
                        }

                    }
                    $data['hairdresserItems']   =  $hairdresserItems;
                }




            }
        }

        return response()->json($level_data,200);
    }

}